# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=hdf5
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_ver=1.12.2
patch=
pkgver=${_ver}${patch//-/.}
pkgrel=2
pkgdesc="General purpose library and file format for storing scientific data (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=("custom")
url="https://www.hdfgroup.org/HDF5/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libaec"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
             )
options=('staticlibs' 'strip')
source=("https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-${_ver%.*}/hdf5-${_ver}${patch}/src/hdf5-${_ver}${patch}.tar.bz2"
        "hdf5-fix-pkgconfig.patch"
        "hdf5-proper-library-names-mingw.patch"
        "hdf5-fix-find-szip.patch")
sha256sums=('1a88bbe36213a2cea0c8397201a459643e7155c9dc91e062675b3fb07ee38afe'
            'f2047b06abba6ec5568878a75c45c7c85cf08ec8e8437439bd23902c1e665bec'
            '7b5595ee9903e14f147f7b4615648cad6a0f5a76f8299461ebe91d739e750476'
            '48bdc4ab0d107bdec11aa3d3a2765cd29eaac5e304c3892365ed2ea352731cbe')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${_ver}${patch}"

  # https://github.com/HDFGroup/hdf5/commit/dcccc355261e305a1d877a798d7fd46556b3cbce.patch
  apply_patch_with_msg \
    hdf5-fix-pkgconfig.patch

    apply_patch_with_msg \
    hdf5-proper-library-names-mingw.patch \
    hdf5-fix-find-szip.patch
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  local -a _enable_fortran
  if [[ "${MINGW_PACKAGE_PREFIX}" == *-clang-* ]]; then
    _enable_fortran="OFF"
  else
    _enable_fortran="ON"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  SZIP_INSTALL=${MINGW_PREFIX} \
    cmake \
      -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_TESTING=OFF \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_SKIP_RPATH=ON \
      -DHDF5_BUILD_HL_LIB=ON \
      -DHDF5_BUILD_CPP_LIB=ON \
      -DHDF5_BUILD_FORTRAN=${_enable_fortran} \
      -DHDF5_BUILD_TOOLS=ON \
      -DHDF5_ENABLE_DEPRECATED_SYMBOLS=ON \
      -DHDF5_ENABLE_SZIP_SUPPORT=ON \
      -DHDF5_ENABLE_Z_LIB_SUPPORT=ON \
      -DHDF5_INSTALL_CMAKE_DIR="lib/cmake" \
      -DHDF5_INSTALL_DATA_DIR="share/doc/hdf5" \
      -DHDF5_MSVC_NAMING_CONVENTION=OFF \
      ../${_realname}-${_ver}${patch}

  cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  if [[ "${MINGW_PACKAGE_PREFIX}" != *-clang-* ]]; then
    mkdir -p ${srcdir}/build-${MSYSTEM}/bin/static/Release/
    mkdir -p ${srcdir}/build-${MSYSTEM}/bin/shared/Release/
    cp ${srcdir}/build-${MSYSTEM}/bin/static/*.mod ${srcdir}/build-${MSYSTEM}/bin/static/Release
    cp ${srcdir}/build-${MSYSTEM}/bin/shared/*.mod ${srcdir}/build-${MSYSTEM}/bin/shared/Release
  fi

  DESTDIR="${pkgdir}" cmake --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/libhdf5.settings
}
